package com.linkvin.ec.hcs.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.linkvin.ec.hcs.dao.model.sysadmin.User;
import com.linkvin.ec.hcs.dao.model.sysadmin.UserRole;
import com.linkvin.ec.hcs.dao.model.sysadmin.bo.UserBo;
import com.linkvin.ec.hcs.service.sysadmin.UserService;
import com.linkvin.ec.hcs.vo.RestVO;
import com.linkvin.ec.hcs.vo.UserRoleRequestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Nicky on 17/11/24.
 */

@Controller
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping(value = "/queryPage",consumes = "application/json")
    @ResponseBody
    public RestVO<Page<UserBo>> queryPage(@RequestBody Page<UserBo> page){
        RestVO<Page<UserBo>> userBoResponseBO = new RestVO<>(0,"success");
        Page<UserBo> userBo = userService.queryPageUser(page);
        userBoResponseBO.setData(userBo);
        return userBoResponseBO;
    }

    @PostMapping(value = "/create",consumes = "application/json")
    @ResponseBody
    public RestVO<User> create(@RequestBody User user){
        RestVO<User> response = new RestVO<>(0,"success");
        try{
            response.setData(userService.addUser(user));
        }catch (Exception e){
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;

    }
    @PostMapping(value = "/update",consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> update(@RequestBody User user){
        RestVO<Integer> response = new RestVO<>(0,"success");
        try{
            response.setData(userService.updateUser(user));
        }catch (Exception e){
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;

    }

    @PostMapping(value = "/delete",consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> delete(@RequestBody List<Integer> userIds){
        RestVO<Integer> response = new RestVO<>(0,"success");
        try{
            response.setData(userService.deleteUser(userIds));
        }catch (Exception e){
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @GetMapping(value = "/ownRole")
    @ResponseBody
    public RestVO<List<Integer>> listOwnRoleIds(@RequestParam("userId") Integer userId){
        RestVO<List<Integer>> response = new RestVO<>(0,"success");
        try{
            List<UserRole> userRoleList = userService.listUserRole(userId);
            if(!Objects.equals(userRoleList,null)){
                List<Integer> roleIds = new ArrayList<>();
                for (UserRole userRole : userRoleList) {
                    roleIds.add(userRole.getRoleId());
                }
                response.setData(roleIds);
            }
        }catch (Exception e){
            response.setCode(10001);
            response.setMessage(e.getMessage());

        }
        return response;
    }

    //TODO 创建角色 zgx
    @PostMapping(value = "/grantRole",consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> grantRole(@RequestBody UserRoleRequestVO reVO){
        RestVO<Integer> result = new RestVO<>(0,"success");
        try {
            int i = userService.assignRole(reVO.getUserId(),reVO.getRoleIds());
            result.setData(i);
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @GetMapping(value = "/listUsers")
    @ResponseBody
    public RestVO<List<UserBo>> listUsers(){
        RestVO<List<UserBo>> response = new RestVO<>(0,"success");
        try{
            response.setData(userService.listUsers());
        }catch (Exception e){
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;
    }
}
